﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.Diagnostics;
using MangaReader.MangaService;


public class KissManga
{
    #region OLD VERSION
    //public static List<string> MangaList;
    string result;
    public struct Chapter
    {
        //public string Url = "";
        //public string Name = "";

        //public static void Chapter(string url, string name)
        //{
        //    Url = url;
        //    Name = name;
        //}
    };

    public string GetWebContent(string url)
    {
        WebClient getMangaList = new WebClient();
        byte[] bytes = getMangaList.DownloadData(url);
        return System.Text.Encoding.GetEncoding("utf-8").GetString(bytes);
    }

    public List<Chapter> GetChapterList(string url)
    {
        List<Chapter> ChapterList = new List<Chapter>();

        //string searchChapter = @"\<(a\shref)[^>]*/Manga/[^>]*>";
        string searchChapter = @"\<(a\shref)[^>]*/Manga/[^>]*>[^>]*>";
        Regex rgx = new Regex(searchChapter, RegexOptions.IgnoreCase);
        MatchCollection chapters = rgx.Matches(url);

        if (chapters.Count > 0)
        {
            foreach (Match chapter in chapters)
            {
                string searchLink = @"";
                string searchName = @"";
                Regex rgxLink = new Regex(searchLink, RegexOptions.IgnoreCase);
                Regex rgxName = new Regex(searchName, RegexOptions.IgnoreCase);
                Match chapterLink = rgxLink.Match(chapter.Value);
                Match chapterName = rgxName.Match(chapter.Value);


            }
        }
        return ChapterList;
    }

    public List<string> GetMangaList(string url)
    {
        try
        {
            WebClient getMangaList = new WebClient();
            byte[] bytes = getMangaList.DownloadData(url);
            result = System.Text.Encoding.GetEncoding("utf-8").GetString(bytes);

            string search2 = @"\<(a\s+class[^>]*href)[^>]*/Manga/[^>]*>";
            Regex rgx = new Regex(search2, RegexOptions.IgnoreCase);
            MatchCollection matches = rgx.Matches(result, 0);

            if (matches.Count > 0)
            {
                List<string> MangaList = new List<string>();
                foreach (Match match in matches)
                {
                    // MangaList.Add(match.Value);
                    string search = @"(?<="")/Manga/.+?(?="")"; // get content inside "content" // /Truyen/mangaName/string = content
                    Regex regex = new Regex(search, RegexOptions.IgnoreCase);
                    Match chapterLink = regex.Match(match.Value);

                    if (chapterLink.Value != "")
                    {
                        MangaList.Add(chapterLink.Value);
                    }
                }
                return MangaList;
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            return null;
        }

        return null;
    }

    public string GetImageLink(string urlManga)
    {
        DownloadHelper helper = new DownloadHelper();
        string webContent = helper.GetHtmlFromUrl(urlManga);
        if (webContent == "")
        {
            return "";
        }
        else
        {
            // http://kissmanga.com/Uploads/Etc/9-12-2011/4140092Final-Fantasy-XII-1-–-page-1.jpg
            string search = @"<(img\s+width)\b[^>]*>*";
            Regex regex = new Regex(search, RegexOptions.IgnoreCase);
            Match match = regex.Match(webContent);

            string patternReturn = @"(?<="")http.+?(?="")";
            Regex rgxReturn = new Regex(patternReturn, RegexOptions.IgnoreCase);
            Match matchReturn = rgxReturn.Match(match.Value);

            return matchReturn.Value;
        }


    }

    public int LastPageOfPager()
    {
        // <a href=\"/MangaList?page=80\" page=\"80\">&raquo; Last </a>
        //href=\"/MangaList?page=80\"
        //string search = @"/MangaList[^>]*Last; // get content inside "content" // /Truyen/mangaName/string = content
        string search = @"page=[^>]*[0-9]*";
        Regex regex = new Regex(search, RegexOptions.IgnoreCase);
        MatchCollection matches = regex.Matches(result, 0);
        //Match chapterLink = regex.Match(result);

        foreach (Match match in matches)
        {
            Debug.WriteLine(match.Value);
        }

        if (matches.Count > 0)
        {
            string lastResult = matches[matches.Count - 1].Value;
            string pattern = @"(\+|-)?[0-9][0-9]*(\.[0-9]*)?";
            Regex reg = new Regex(pattern);
            Match match = reg.Match(lastResult);
            //MatchCollection matches11 = reg.Matches(lastResult, 0);

            if (match != null)
                return int.Parse(match.Value);
        }
        return 1;
    }
    #endregion
    const string KEY = "vnsharing";
    string _url;
    MangaServiceClient service;

    public string Url
    {
        get { return _url; }
        set { _url = value; }
    }

    public KissManga(string url)
    {
        _url = url;
        service = new MangaServiceClient();
    }



    internal MangaStoryDetailContract GetManga()
    {
        return service.GetMangaStoryDetail(KEY, this.Url);
    }

}

